'''
Created on 2012/07/03

@author: harendra
'''
from basehandler.BaseHandler import BaseHandler
from datamodels.UserInfo import UserInfo
from datamodels.RssItem import RssItem
import logging

class DefaultFeed(BaseHandler):
    def get(self):
        context={};
        self.render_response('instafeeder.html',**context)
        
class FeedListGetter():
    def getFeeds(self,username,password):
        logging.error(username)
        logging.error(password)
        userinfo=UserInfo.get_by_key_name(username)
        logging.error(userinfo)
        logging.error(userinfo.rssList)
        if userinfo==None:
            userinfo=UserInfo(key_name=username)
            userinfo.instapaperusername=username
            userinfo.instapaperpassword=password
            userinfo.put()
            return self.getDefaultFeeds()
        if userinfo.instapaperpassword!=password:
            userinfo.instapaperpassword=password
            userinfo.put()
        rsslist=userinfo.rssList
        userfeeds=self.getUserFeeds(rsslist)
        return userfeeds
        
    def getDefaultFeeds(self):
        feednames=['arstechnica','cnet','engadget','lifehacker','macworld','mashable','slashdot','techcrunch','theverge','wired','bbc','cnn','reuters']
        feedimages=['arstechnica.png','cnet.png','engadget.png','lifehacker.jpg','macworld.gif','mashable.jpg','slashdot.jpg','techcrunch.png','theverge.png','wired.gif','bbc.jpg','cnn.jpg','reuters.gif']
        feedlinks=['verge','slashdot','wired']
        feedtype=['tech','tech','tech','tech','tech','tech','tech','tech','tech','tech','news','news','news']
        feeds={}
        for i in range(len(feednames)):
            current={}
            current['feedname']=feednames[i]
            current['feedurl']='link'
            current['feedimage']='/images/'+feedimages[i]
            current['feedtype']=feedtype[i]
            current['subscribed']='notsubscribed'
            feeds[feednames[i]]=current
        return feeds
            
    
    def getUserFeeds(self,rsslist):
        allfeeds=[]
        pickedfeeds=[]
        feeds=self.getDefaultFeeds()
        for rss in rsslist:
            currentfeed=RssItem.get_by_key_name(rss)
            if currentfeed!=None:
                feedname=currentfeed.name
                if(feeds.has_key(feedname)):
                    feeds[feedname]['subscribed']='subscribed'
                    feeds['feedid']=rss
                else:
                    feedurl=currentfeed.url
                    feedsettings={'subscribed':'subscribed','feedname':feedname,'feedurl':feedurl,'feedid':rss,'feedtype':'custom'}
                    feeds[feedname]=feedsettings
        return feeds    

class FeedDownloader():
    pass

class DownloadUpdateSender():
    pass
